<%-- 
    Document   : editGoods
    Created on : Oct 28, 2011, 2:12:02 AM
    Author     : Admin
--%>

<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="manufacturerBean.ManufacturerHome"%>
<%@page import="manufacturerBean.Manufacturer"%>
<%@page import="goodsBean.GoodsHome"%>
<%@page import="categoryBean.Category"%>
<%@page import="categoryBean.CategoryHome"%>
<%@page import="goodsBean.Goods"%>
<%@page import="javax.naming.InitialContext"%>
<%@page import="java.util.Set"%>
<%@page import="java.util.HashMap"%>
<%@page import="DBConnection.ATeamObject"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page errorPage = "Error.jsp" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Изменение параметров товара</title>
        <%  InitialContext initialContext;
            GoodsHome goodsHome;
            Goods goods;
            Object homeObject;

            CategoryHome categoryHome;
            Category category;
            Object homeObject1;

            Manufacturer manufacturer;
            ManufacturerHome manufacturerHome;
            Object homeObject2;

            ATeamObject ato = new ATeamObject();

            initialContext = new InitialContext();
            homeObject = initialContext.lookup("GoodsBean");
            goodsHome = (GoodsHome) PortableRemoteObject.narrow(homeObject,
                    GoodsHome.class);

            homeObject1 = initialContext.lookup("CategoryBean");
            categoryHome = (CategoryHome) PortableRemoteObject.narrow(
                    homeObject1, CategoryHome.class);

            homeObject2 = initialContext.lookup("ManufacturerBean");
            manufacturerHome = (ManufacturerHome)PortableRemoteObject.
                    narrow(homeObject2,ManufacturerHome.class);
        %>
    </head>
    <body background="1.jpg" bgproperties="fixed">
        <h4>
            <a href="index.jsp">Главная</a> / 
            <a href="editGoods.jsp?goodID=<%=request.getParameter("goodID")%>">Изменение параметров товара</a>
        </h4>
        <h1>Изменение параметров товара</h1>
        <%  if (request.getParameter("f") != null) {
                Integer goodsId = Integer.parseInt(
                        request.getParameter("goodID"));
                Integer categoryId = Integer.parseInt(
                        request.getParameter("category"));
                Integer manufacturerId = Integer.parseInt(
                        request.getParameter("manufacturer"));
                Integer goodsPrice = Integer.parseInt(
                        request.getParameter("price"));
                Integer goodsNumber = Integer.parseInt(
                        request.getParameter("number"));
                String goodsName = request.getParameter("name");
                String goodsDescription = request.getParameter("description");
                
                goods = goodsHome.findByPrimaryKey(goodsId);
                
                if (!goods.getNameOfGoods().equals(goodsName)) {
                    goods.setNameOfGoods(goodsName);
                }
                
                if (!goods.getGoodsDescription().equals(goodsDescription)) {
                    goods.setGoodsDescription(goodsDescription);
                }
                
                
                if (goods.getCategoryId() != categoryId) {
                    category = categoryHome.findByPrimaryKey(categoryId);
                    goods.setCategoryId(category.getObjectId());
                }    
                
                if (goods.getManufactureId() != manufacturerId) {
                    manufacturer = manufacturerHome.findByPrimaryKey(
                            manufacturerId);
                    goods.setManufactureId(manufacturer.getObjectId());                
                }
                
                if (goods.getPrice() != goodsPrice) {
                    goods.setPrice(goodsPrice);
                }
                
                if (goods.getNumber() != goodsNumber) {
                    goods.setNumber(goodsNumber);
                }
                
                response.sendRedirect("index.jsp");
            } else {
                String goodsID = request.getParameter("goodID");
                goods = goodsHome.findByPrimaryKey(Integer.parseInt(goodsID));                
        %>
        <form>
            <p> Название товара
                <input type="text" name="name" value="<%= goods.getNameOfGoods() %>" size="25" />
            </p>
            <p> Описание товара
                <input type="text" name="description" value="<%= goods.getGoodsDescription() %>" size="25" />
            </p>
            <p> Категория товара
                <select name="category">
                    <%  Collection allCategory = categoryHome.findAll();
                        Iterator iter = allCategory.iterator();
                        while(iter.hasNext()){
                            category = (Category)iter.next();
                            if (category.getObjectId() == goods.getCategoryId()) {
                    %>
                                <option value="<%= category.getObjectId() %>" selected> <%= category.getName() %></option>
                            <%
                            } else {
                            %>
                                <option value="<%= category.getObjectId() %>"><%= category.getName() %></option>
                    <%
                            };
                        };
                    %>
                </select>
            </p>    
            <p> Производитель
                <select name="manufacturer">
                    <%  Collection allManufacturer = manufacturerHome.findAll();
                        iter = allManufacturer.iterator();
                        while(iter.hasNext()){
                            manufacturer = (Manufacturer)iter.next();
                            if (manufacturer.getObjectId() == goods.getManufactureId()) {
                    %>
                                <option value="<%= manufacturer.getObjectId() %>" selected> <%= manufacturer.getName() %></option>
                                <%
                            } else {
                                %>
                                <option value="<%= manufacturer.getObjectId() %>"><%= manufacturer.getName() %></option>
                    <%
                            };
                        };
                    %>
                </select>
            </p>
            <p> Цена
                <input type="text" name="price" value="<%= goods.getPrice() %>" size="25" />
            </p>
            <p> Количество
                <input type="text" name="number" value="<%= goods.getNumber() %>" size="25" />
            </p>
            <p>
                <button type="submit"
                        formaction="editGoods.jsp?goodID=<%= goodsID %>&f=1"
                        formmethod="POST" style="width:150px;height:55px">
                                    Подтвердить <br> изменение
                </button>
            </p>
        </form> 
        <%
            };
        %>
    </body>
</html>
